package GUI_player;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.Insets;

import javax.swing.JPanel;

import logic.*;
import GUI.*;

import java.awt.GridBagLayout;


public class GamePanelPlayer extends JPanel {


	private static final long serialVersionUID = 1L;
	public Color[] cores = {Color.white,Color.red,Color.blue,Color.green,Color.yellow,Color.orange,
			Color.GRAY,Color.cyan,Color.magenta,Color.darkGray, Color.LIGHT_GRAY,
			Color.PINK, new Color(159,95,159), new Color(92,64,51),new Color(47,79,47),new Color(235,199,158)};
	public flowfree fl;
	public CellPlayer[][] boardCells;
	static public GamePanelPlayer self;
	public GamePanelPlayer() {

		self = this;

		buildBoard(fl);	

		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		GUI.frame.getContentPane().removeAll();
		GUI.frame.getContentPane().add(this, gbc_panel);




	}
	public void cleanBoard()
	{
		for(int i =0; i< fl.matrix.length;i++)
		{
			for(int j = 0 ; j< fl.matrix.length;j++){
				cleanColor(boardCells[i][j].getColor());
				boardCells[i][j].repaint();
			}
		}
		CellPlayer.usingColor=null;
		CellPlayer.lastCell=null;
	}
	public void buildBoard(flowfree fl)
	{
		if(fl == null)
			return;
		this.fl=fl;

		removeAll();
		GridBagLayout gridBagLayout2 = new GridBagLayout();
		gridBagLayout2.columnWidths = new int[]{0};
		gridBagLayout2.rowHeights = new int[]{0};
		gridBagLayout2.columnWeights = new double[]{Double.MIN_VALUE};
		gridBagLayout2.rowWeights = new double[]{Double.MIN_VALUE};

		setLayout(gridBagLayout2);
		boardCells = new CellPlayer[fl.matrix.length][fl.matrix.length];
		for(int i =0; i< fl.matrix.length;i++)
		{
			for(int j = 0 ; j< fl.matrix.length;j++){

				CellPlayer cellPlayer = new CellPlayer(i,j,fl.matrix[i][j],cores[fl.matrix[i][j]%100]);


				GridBagConstraints gbc_lblCell = new GridBagConstraints();
				gbc_lblCell.insets = new Insets(0, 0, 0, 0);
				gbc_lblCell.gridx = j;
				gbc_lblCell.gridy = i;
				gbc_lblCell.ipady = 0;
				gbc_lblCell.ipadx = 0;


				add(cellPlayer, gbc_lblCell);
				boardCells[i][j] = cellPlayer;
			}
		}
	}
	public boolean canPlay(pos initPos, pos finalPos)
	{
		if(!initPos.adjacent(finalPos))
			return false;
		else
		{
			int valIni=boardCells[initPos.getX()][initPos.getY()].getValue();
			int valFinal=boardCells[finalPos.getX()][finalPos.getY()].getValue();

			if(valIni>100 &&
					(valFinal ==0 ||(valIni>100 && valFinal>100 && valIni%100 == valFinal%100)))
				return true;
			else return false;
		}

	}
	public void cleanColor(Color c)
	{
		boolean initFound = false;
		for(int i =0; i< boardCells.length;i++)
		{
			for(int j = 0 ; j< boardCells.length;j++){

				if(boardCells[i][j].getColor()== c)
					if(boardCells[i][j].isEndPoint() && !initFound)
					{
						initFound = true;
						int val = (boardCells[i][j].getValue()%100)+100;
						boardCells[i][j].setValue(val);
						fl.matrix[i][j]=val;
					}
					else if(boardCells[i][j].isEndPoint() && initFound)
					{
						int val = (boardCells[i][j].getValue()%100)+200;
						boardCells[i][j].setValue(val);
						fl.matrix[i][j]=val;
					}
					else{
						boardCells[i][j].setValue(0);
						boardCells[i][j].setColor(Color.WHITE);
						fl.matrix[i][j]=0;
					}
			}
		}
	}
	public boolean intersect(pos position,Color c)
	{
		CellPlayer cellPlayer = boardCells[position.getX()][position.getY()];
		if(cellPlayer.getValue()<100 && !cellPlayer.getColor().equals(c) )
			return true;
		else return false;
	}
	public boolean  reversePlay(pos initPos, pos finalPos,pos previous)
	{
		if(!initPos.adjacent(finalPos))
			return false;
		else
		{
			if(previous==null)
				return true;
			if(!finalPos.equals(previous) )
				return false;
			int valIni=boardCells[initPos.getX()][initPos.getY()].getValue();
			int valFinal=boardCells[finalPos.getX()][finalPos.getY()].getValue();
			if(valFinal<100 && valIni>100 && valIni%100 == valFinal%100) 
				return true;
			else return false;
		}
	}
	public boolean checkEndGame()
	{
		for(int i =0; i< boardCells.length;i++)
		{
			for(int j = 0 ; j< boardCells.length;j++)
			{
				if(boardCells[i][j].getValue() >100)
					return false;
			}
		}
		return true;

	}
}
